package hipparcos.model;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class HipparcosReader
{
	ArrayList<HipparcosStar> list = new ArrayList<HipparcosStar>();
	String path="";
	
	public HipparcosReader(String path)
	{
		this.path=path;
	}

	public static void main(String[] args) throws IOException {

		String path="C:/Documents/I_239_tyc_main.dat.gz.txt";
		//String path="C:/Documents/I_239.txt";
		
		HipparcosReader hipparcosReader = new HipparcosReader(path);
		hipparcosReader.process();
		ArrayList<HipparcosStar> list = hipparcosReader.getStars();
		System.out.println(list.size());
	}

	private ArrayList<HipparcosStar> getStars()
	{
		return list;
	}

	private void process() throws IOException
	{

		BufferedReader reader = null;
		try
		{
			reader = new BufferedReader(new FileReader(path));
			String line;
			int lineNumber=0;
			while ((line = reader.readLine()) != null)
			{
				lineNumber++;
				if (lineNumber<13) 
					continue;
				if (line.contains("---") || line.length()<345)
					continue;
				HipparcosStar hs = elaborateParts(line);	
				if(lineNumber%1e3 ==0)
					System.out.println(lineNumber);
				list.add(hs);
			}
			System.out.println("Total line read = " + lineNumber);
		}
		catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		finally{
			if (reader!=null);
			reader.close();
		}
		
	}

	private HipparcosStar elaborateParts(String line)
	{
		HipparcosStar hs = new HipparcosStar();
		hs.Catalog=getString(line, 1);
		TYC tyc =new TYC();
		tyc.tyc1=getInt(line, 3,6);
		tyc.tyc2=getInt(line, 7,12);
		tyc.tyc3=getInt(line, 13,14);
		hs.tyc=tyc;
		
		hs.Proxy=getString(line, 16);
	
		hs.Vmag=getDouble(line, 42,47);
		hs.r_Vmag=getString(line, 48);
		hs.RAdeg=getDouble(line, 50,61);
		hs.DEdeg=getDouble(line, 63,74);
		hs.AstroRef=getString(line, 76);
		hs.Plx=getDouble(line, 78,84);
		hs.pmRA=getDouble(line, 86,93);
		
		hs.pmDE=getDouble(line, 95,102);
		
		hs.e_RAdeg=getDouble(line, 104,109);
		hs.e_DEdeg=getDouble(line, 111,116);
		hs.e_Plx=getDouble(line, 118,123);
		hs.e_pmRA=getDouble(line, 125,130);
		hs.e_pmDE=getDouble(line, 132,137);
		hs.DE_RA=getDouble(line, 139,143);
		hs.Plx_RA=getDouble(line, 145,149);
		hs.Plx_DE=getDouble(line, 151,155);
		hs.pmRA_RA=getDouble(line, 157,161);
		hs.pmRA_DE=getDouble(line, 163,167);
		hs.pmRA_Plx=getDouble(line, 169,173);
		hs.pmDE_RA=getDouble(line, 175,179);
		hs.pmDE_DE=getDouble(line, 181,185);
		hs.pmDE_Plx=getDouble(line, 187,191);
		hs.pmDE_pmRA=getDouble(line, 193,197);
		hs.Nastro=getDouble(line, 199,201);
		hs.F2=getDouble(line, 203,207);
		
		hs.HIP=getInt(line, 209, 214);
		hs.BTmag=getDouble(line, 216,221);
		hs.e_BTmag=getDouble(line, 223,227);
		hs.VTmag=getDouble(line, 229,234);
		hs.e_VTmag=getDouble(line, 236,240);
		hs.r_BTmag=getString(line, 242);
		
		
		hs.BminusV=getDouble(line, 244,249);
		hs.e_BminusV=getDouble(line, 251,255);
		hs.Q=getInt(line, 257);
		hs.Fs=getDouble(line, 259,262);
		hs.Source=getString(line, 264);
		
		
		hs.Nphoto=getInt(line, 266,268);
		hs.VTscat=getDouble(line, 270,274);
		hs.VTmax=getDouble(line, 276,280);
		hs.VTmin=getDouble(line, 282,286);
		
		hs.Var=getString(line, 288);
		hs.VarFlag=getString(line, 290);
		hs.MultFlag=getString(line, 292);
		hs.morePhoto=getString(line, 294);
		hs.m_HIP=getString(line, 296,297);
		
		hs.PPM=getInt(line, 299,304);
		hs.HD=getInt(line, 306,311);
		
		hs.BD=getString(line, 313,322);
		hs.CoD=getString(line, 324,333);
		hs.CPD=getString(line, 335,344);
		if (line.length()>346)
			hs.Remark=getString(line, 346);

		return hs;
	}

	private String getString(String line, int i)
	{
		return line.substring(i-1, i);
	}
	
	private String getString(String line, int i, int j)
	{
		return line.substring(i-1, j);
	}

	private double getDouble(String line, int i, int j)
	{
		String value = line.substring(i-1,j).trim();
		if(value.length()==0)
			return -999999999;
		try
		{

			return Double.parseDouble(value);
		}
		catch (Exception e)
		{
			System.out.println("can not parse:["+value+"]");
		}
		return -999999999;
	}

	private int getInt(String line, int i)
	{
		String value = line.substring(i-1,i).trim();
		if(value.length()==0)
			return -999999999;
		try
		{

			return Integer.parseInt(value);
		}
		catch (Exception e)
		{
			System.out.println("can not parse:["+value+"]");
		}
		return -999999999;
	}
	private int getInt(String line, int i, int j)
	{
		String value = line.substring(i-1,j).trim();
		if(value.length()==0)
			return -999999999;
		try
		{

			return Integer.parseInt(value);
		}
		catch (Exception e)
		{
			System.out.println("can not parse:["+value+"]");
		}
		return -999999999;
	}

}
